namespace SimpleWebServices.Web.UI.Html.Elements
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Xml;
    using System.Xml.Serialization;

    [SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Option", Justification = "The naming is intentionally the same as HTML.")]
    [XmlRoot("option")]
    public class Option : Element, IOption, IAttributeDisabled, IXmlSerializable
    {
        public Option()
        {
        }

        public Option(string value, string title, string text)
        {
            this.Text = text;
            this.Title = title;
            this.Value = value;
        }

        public bool Disabled
        {
            get;
            set;
        }

        public string Label
        {
            get;
            set;
        }

        public bool Selected
        {
            get;
            set;
        }

        public string Text
        {
            get;
            set;
        }

        public string Value
        {
            get;
            set;
        }

        public override IElement Deserialize(string name, string type, string xml)
        {
            IElement result = null;

            if (null == type && "option".Equals(name, StringComparison.OrdinalIgnoreCase))
            {
                result = xml.Deserialize<Option>();
            }

            return result;
        }

        public override void ReadXml(XmlReader reader)
        {
            base.ReadXml(reader);

            if (XmlNodeType.Element == reader.NodeType)
            {
                string value = reader.GetAttribute("label");
                if (!string.IsNullOrEmpty(value))
                {
                    this.Label = value;
                }

                value = reader.GetAttribute("value");
                if (!string.IsNullOrEmpty(value))
                {
                    this.Value = value;
                }

                this.Disabled = "disabled".Equals(reader.GetAttribute("disabled"), StringComparison.OrdinalIgnoreCase);
                this.Selected = "selected".Equals(reader.GetAttribute("selected"), StringComparison.OrdinalIgnoreCase);

                this.Text = reader.ReadString();
            }
        }

        public override void WriteXml(XmlWriter writer)
        {
            base.WriteXml(writer);

            if (!string.IsNullOrEmpty(this.Label))
            {
                writer.WriteAttributeString("label", this.Label);
            }

            if (!string.IsNullOrEmpty(this.Value))
            {
                writer.WriteAttributeString("value", this.Value);
            }

            if (this.Disabled)
            {
                writer.WriteAttributeString("disabled", "disabled");
            }

            if (this.Selected)
            {
                writer.WriteAttributeString("selected", "selected");
            }

            writer.WriteString(this.Text ?? string.Empty);
        }
    }
}